package com.vaifa.expressage.controller;

import com.vaifa.expressage.service.expressageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping
public class expressageController {
    @Autowired
    private expressageService service;

    @PostMapping("api")
    public void importData(MultipartFile file) throws Exception {
        System.out.println(file.getOriginalFilename());
        this.service.importData(file);
    }

    @PostMapping("auto")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResponseEntity<String> AutoData(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Excel文件不能为空");
            }
            // 调用服务层方法处理文件
            String result = service.AutoData(file);
            if (result != null) {
                return ResponseEntity.ok("Excel自动生成完成");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel文件处理失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel服务器错误: " + e.getMessage());
        }
    }
}
